home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q]Assigning function pointer in C/C++.
- Date: 22 Jan 1996 22:32:40 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan22173240@g7240065.bridge.bst.bls.com>
- References: <DL3JJu.5nB.0.queen@torfree.net> <4doc42$gsb@bmdhh222.bnr.ca>
- <ALUN.CHAMPION.96Jan19113523@g7240065.bridge.bst.bls.com>
- <4e09re$kit@populus.slu.se>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: Thomas.Johansson@stax.slu.se's message of 22 Jan 1996 15:18:06
- GMT
-
- In article <4e09re$kit@populus.slu.se> Thomas.Johansson@stax.slu.se (Thomas Johansson) writes:
- : In article <ALUN.CHAMPION.96Jan19113523@g7240065.bridge.bst.bls.com>, Alun.Champion@bridge.bst.bls.com (Alun Champion) says:
-
- :> You can have pointers to member functions providing you know what class
- :> (or base class) you are going to be assigning functions from.
- :>
- :> int (A::*p)(void);
- :>
- :> declares p as a pointer to an A member function which takes nothing and
- :> returns an int. The usage of p requires an object or pointer to an object of
- :> type A or derived from A. i.e:
- :>
- :> A a;
- :> int z = (a.*p)(); // Uses '.*' operator.
- :> or
-
- : Is there any way that you can declare a variable to hold the resulting
- : pointer, or must it be 'used' right away ?
- : something like
-
- : typdef int (A::*p)(void) Fp; // Fp is a type of 'pointer to member function'
-
- : Fp f = (a.*p); // the object a (of class A) has a member p
- : int z = f();
-
- : I cannot make this work with Borland 4.5.
-
- I think you may have misunderstood my original post.
-
- Example:
-
- #include <iostream.h>
-
- class A
- {
- public:
- A(int a = 0) : a_(a)
- { }
-
- int func(void)
- { return a_; }
-
- int foo(void)
- { return -a_; }
-
- private:
- int a_;
- };
-
- int
- main(void)
- {
- A a(5);
- int (A::*p)(void) = A::func; // p is a variable pointing to a member
- // function of A returning an int
- cout << (a.*p)() << endl; // apply p to object a.
-
- p = A::foo; // re-assign the variable p.
- cout << (a.*p)() << endl;
-
- return 0;
- }
-
- Sorry, it just doesn't work the way you would like it. You can only make
- a pointer to the class method and not to the instance method. Therefore
- you need an instance to apply this method on.
-
- You could try a small class to do this for you:
-
- template <class T>
- class IntFunctor
- {
- public
- IntFunctor(T& object, int (T::*method)(void))
- : object_(object), method_(method)
- { }
-
- int operator () (void)
- { return (obj.*method)(); }
-
- private:
- T& object_;
- int (T::*method_)(void);
- };
-
- Then you could use it like:
-
- A a;
- IntFunctor f(a, A::func);
- ...
- int z = f();
-
- But it is not very generic and seems a bit of a hassle, and it has some
- potential risks, like the instance goes away and a use of the Functor after
- that would have undefined results (usually at least crashing the program)';
-
- Hope this clears things up.
- Regards
-
- -A.
- --
- | A.Champion |
-